package com.richapp.launcher.database.system.dao;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.richapp.launcher.database.system.SystemBaseDao;
import com.richapp.launcher.database.system.mode.UserMachineButton;

import java.util.List;

public class UserMachineButtonDao extends SystemBaseDao<UserMachineButton>{

	public UserMachineButtonDao(Context context) {
		super(context,UserMachineButton.class);
	}

    public UserMachineButton get(String machineName,int buttonId){
        UserMachineButton userMachineButton = null;
        try {
            QueryBuilder queryBuilder = gettOp().queryBuilder();
            queryBuilder.where().eq("machineName",machineName).and().eq("buttonId",buttonId);
            Object obj = queryBuilder.queryForFirst();
            if(obj instanceof UserMachineButton){
                userMachineButton = (UserMachineButton)obj;
            }
        }catch (Exception e){
            Log.e(getClass().getName(), e.getMessage());
        }
        return userMachineButton;
    }

    public List<UserMachineButton> getList(String machineName){
        List<UserMachineButton> userMachineButtonList = null;
        try {
            QueryBuilder queryBuilder = gettOp().queryBuilder();
            queryBuilder.where().eq("machineName",machineName);
            queryBuilder.orderBy("buttonId",true);
            userMachineButtonList = queryBuilder.query();
        }catch (Exception e){
            Log.e(getClass().getName(), e.getMessage());
        }
        return userMachineButtonList;
    }

    public void del(String machineName){
        try {
            DeleteBuilder deleteBuilder = gettOp().deleteBuilder();
            deleteBuilder.where().eq("machineName",machineName);
            deleteBuilder.delete();
        }catch (Exception e){
            Log.e(getClass().getName(), e.getMessage());
        }
    }

}
